﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Cocos2D;
using CCPoint = Cocos2D.CCPoint;

namespace TestTiger.PlayingCard
{
    public enum CardColor
    {
        黑桃 = 0,
        樱花,
        红桃,
        方片,
        背面
    }

    public class PlayingCards : CCLayer
    {
        /// <summary>
        /// 扑克牌图片列表
        /// </summary>
        protected List<PokerCard> Cards;
        /// <summary>
        /// 所有的扑克牌集合
        /// </summary>
        protected PokerCard[,] AllCards;

        /// <summary>
        /// 扑克牌面最终的结果
        /// 11==J
        /// 12==Q
        /// 13==K 
        /// </summary>
        public int ResoutNumber { get; set; }
        /// <summary>
        /// 结果花色
        /// </summary>
        public CardColor ResoutColor { get; set; }

        /// <summary>
        /// 当前的扑克牌
        /// </summary>
        public PokerCard CurrentCard { get; private set; }
        /// <summary>
        /// 下一张
        /// </summary>
        private PokerCard _nextCard;

        /// <summary>
        /// 是否是停止状态
        /// </summary>
        public bool IsStop { get; private set; }


        #region 时间和状态控制
        /// <summary>
        /// 当前的状态
        /// 0 == 停止
        /// 1 == 加速
        /// 2 == 减速
        /// </summary>
        private byte _state = 0;

        private float useTime;
        #endregion

        #region 速度控制

        //最大速度
        private float _maxspeed;
        //最慢速度
        private float _lowspeed;
        ////当前速度
        //private float _speed= 1f;
        //速度增长/减小因子
        private float _speedfactor;
        /// <summary>
        /// 最大速度
        /// </summary>
        public float MaxSpeed
        {
            get { return _maxspeed; }
            set { if (value > _lowspeed) _maxspeed = value; }
        }
        /// <summary>
        /// 最慢速度
        /// </summary>
        public float LowSpeed
        {
            get { return _lowspeed; }
            set { if (_lowspeed < _maxspeed) _lowspeed = value; }
        }
        /// <summary>
        /// 加减速因子
        /// </summary>
        public float SpeedFactor
        {
            get { return _speedfactor; }
            set { if (value > 0) _speedfactor = value; }
        }

        #endregion



        /// <summary>
        /// 当滚动到指定结果后触发
        /// </summary>
        public event EventHandler OnScrollFinsish;


        private Random rd = new Random();
        private CCPoint outPoint;
        private CCPoint showPoint;
        private CCPoint nextCardPosition;

        public PlayingCards(string[] cardsPath, CCPoint pt, CCSize cardSize)
        {

            //this.Cards = cards.ToList();
            this.Cards = new List<PokerCard>(3);
            this.AllCards = new PokerCard[14, 4];

            nextCardPosition = new CCPoint(pt.X, pt.Y + cardSize.Height);
            outPoint = new CCPoint(pt.X, pt.Y - cardSize.Height);
            showPoint = pt;

            //创建扑克牌列表对象，默认只显示第一张
            for (int i = 0; i < cardsPath.Length; i++)
            {
                if (i == 0)
                {
                    var zeroCard = new PokerCard(cardsPath[0], pt, 0, CardColor.背面, true);
                    AllCards[0, 0] = zeroCard;
                    AllCards[0, 1] = zeroCard;
                    AllCards[0, 2] = zeroCard;
                    AllCards[0, 3] = zeroCard;

                }
                else
                {
                    var index = i + 3;
                    int cum = index % 4;
                    int row = (index / 4);
                    AllCards[row, cum] = new PokerCard(
                        cardsPath[i],
                       nextCardPosition,
                        row,
                        (CardColor)cum,
                        false);
                }
            }
            Cards.Add(AllCards[0, 0]); //当前扑克牌
            Cards.Add(AllCards[1, rd.Next(0, 4)]); //下一张
            //设置当前的显示扑克牌
            CurrentCard = Cards[0];
            _nextCard = Cards[1];
            foreach (var s in Cards)
                AddChild(s);


        }

        public override bool Init()
        {
            this.ContentSize = new CCSize(48, 108);
            return base.Init();
        }

        private float speedRate;
        public float SpeedRate
        {
            get
            {
                return speedRate;
            }
            set
            {
                speedRate = value;
                speed1.Speed = speedRate;
                speed2.Speed = speedRate;
            }
        }
        private CCSpeed speed1, speed2;

        private void RemoveCard()
        {
            // CurrentCard.Position = nextCardPosition;
            RemoveChild(this.CurrentCard, true);
            Cards.RemoveAt(0);
        }

        private void CreateCard()
        {
            //do
            //{
            CurrentCard = _nextCard;
            SetCurrentCardAction();

            //如果还要继续就创建

            //随机一个花色
            var color = rd.Next(0, 4);
            var next = AllCards[rd.Next(1, 14), color];
            next.Position = nextCardPosition;

            Cards.Add(next);

            AddChild(next);
            _nextCard = next;
            _nextCard.Position = new CCPoint(0, 0);
            SetNextCardAction();
            //} while (CurrentCard == null);
        }

        public override void Visit()
        {
            base.Visit();
        }

        private void SetCurrentCardAction()
        {
            if (CurrentCard == null) throw new Exception("CurrentCard is Null");
            speed1 = new CCSpeed(CCSequence.FromActions(new CCMoveBy(1, new CCPoint(0, -108)), new CCDelayTime(.5f), new CCCallFunc(RemoveCard)), SpeedRate);
            this.CurrentCard.RunAction(speed1);
        }
        private void SetNextCardAction()
        {
            if (_nextCard == null) throw new Exception("_nextCard is Null");
            speed2 = new CCSpeed(CCSequence.FromActions(new CCMoveBy(1, new CCPoint(0, -108)), new CCCallFunc(CreateCard)), SpeedRate);
            this._nextCard.RunAction(speed2);
        }

        public void StartLoop()
        {
            _state = 1; //状态改为加速
            speedRate = 5f;
            SetCurrentCardAction();
            SetNextCardAction();
        }

        /// <summary>
        /// 将扑克牌停在制定的结果上
        /// </summary>
        /// <param name="resout"></param>
        public void StopWithResout(int resout, CardColor color)
        {
            _state = 2;//设置为减速
            ResoutNumber = resout;
            ResoutColor = color;
        }

        /// <summary>
        /// 将扑克牌停止在随机的结果处
        /// </summary>
        public void StopWithRandom()
        {
            StopWithResout(rd.Next(1, 13), (CardColor)rd.Next(0, 4));
        }

    }
}
